package cn.jekun.web.shop.action;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;











import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.EncryptUtil;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.DUnit;
import cn.jekun.component.core.bean.gen.ErpBarcode;
import cn.jekun.component.core.bean.gen.ErpBarcodeImage;
import cn.jekun.component.core.bean.gen.ErpInventory;
import cn.jekun.component.core.bean.gen.ErpMerchandiseImage;
import cn.jekun.component.core.bean.gen.ErpMerchandiseType;
import cn.jekun.component.core.bean.gen.ErpSalePrice;
import cn.jekun.component.core.bean.gen.RsVendor;
import cn.jekun.component.core.service.gen.DUnitService;
import cn.jekun.component.core.service.gen.ErpBarcodeImageService;
import cn.jekun.component.core.service.gen.ErpBarcodeService;
import cn.jekun.component.core.service.gen.ErpInventoryService;
import cn.jekun.component.core.service.gen.ErpMerchandiseImageService;
import cn.jekun.component.core.service.gen.ErpMerchandiseTypeService;
import cn.jekun.component.core.service.gen.ErpSalePriceService;
import cn.jekun.component.core.service.gen.RsVendorService;
import cn.jekun.web.shop.bean.Merchandise;
import cn.jekun.web.shop.bean.MerchandiseType;
import cn.jekun.web.shop.service.MerchandiseService;

/**
 * 商品管理后台
 * @author hc
 *
 */
@SuppressWarnings("serial")
public class MerchandiseAction extends ShopBaseAction {
	private static final String AUTH_SHOP_INVENTORY_MANAGER="SHOP_INVENTORY_MANAGER";//库存管理
	@Resource(name=MerchandiseService.BEAN_NAME)
	MerchandiseService merchandiseService;
	
	@Resource(name=DUnitService.BEAN_NAME)
	DUnitService dUnitService;
	
	@Resource(name=ErpInventoryService.BEAN_NAME)
	ErpInventoryService erpInventoryService;
	
	@Resource(name=ErpSalePriceService.BEAN_NAME)
	ErpSalePriceService erpSalePriceService;
	
	@Resource(name=RsVendorService.BEAN_NAME)
	RsVendorService rsVendorService;
	
	@Resource(name=ErpMerchandiseImageService.BEAN_NAME)
	ErpMerchandiseImageService erpMerchandiseImageService;
	
	@Resource(name=ErpMerchandiseTypeService.BEAN_NAME)
	ErpMerchandiseTypeService erpMerchandiseTypeService;
	
	@Resource(name=ErpBarcodeService.BEAN_NAME)
	ErpBarcodeService erpBarcodeService;
	
	@Resource(name=ErpBarcodeImageService.BEAN_NAME)
	ErpBarcodeImageService erpBarcodeImageService;
	
	private Merchandise merchandise;//商品数据
	private ErpInventory erpInventory;//库存数据
	private ErpSalePrice erpSalePrice;//商品价格数据
	private ErpMerchandiseType erpMerchandiseType;//商品分类数据
	private ErpMerchandiseImage erpMerchandiseImage;
	private List<DUnit> dUnitsList;//商品单位List
	private List<RsVendor> rsVendorsList; //生产厂家List
	private List<ErpMerchandiseImage> goodsPicList;//商品图片List
	private List<ErpMerchandiseType> erpMerchandiseTypesList;//商品分类List
	private String merStatus;//商品状态
	private String keywords;//关键字
	private String keywordsType;//关键字类型
	private String defImgPath;//默认图片地址
	private List<String> checkbox_merchandise;
	
	private List<String> merchandiseTypeIdArr;//商品分类ID数组
	private List<String> merchandiseTypeParentIdArr;//商品分类父级ID数组
	private List<String> merchandiseTypeSortArr;//商品分类排序数组
	
	
	
	public List<String> getMerchandiseTypeIdArr() {
		return merchandiseTypeIdArr;
	}

	public void setMerchandiseTypeIdArr(List<String> merchandiseTypeIdArr) {
		this.merchandiseTypeIdArr = merchandiseTypeIdArr;
	}

	public List<String> getMerchandiseTypeParentIdArr() {
		return merchandiseTypeParentIdArr;
	}

	public void setMerchandiseTypeParentIdArr(
			List<String> merchandiseTypeParentIdArr) {
		this.merchandiseTypeParentIdArr = merchandiseTypeParentIdArr;
	}

	public List<String> getMerchandiseTypeSortArr() {
		return merchandiseTypeSortArr;
	}

	public void setMerchandiseTypeSortArr(List<String> merchandiseTypeSortArr) {
		this.merchandiseTypeSortArr = merchandiseTypeSortArr;
	}

	public List<String> getCheckbox_merchandise() {
		return checkbox_merchandise;
	}

	public void setCheckbox_merchandise(List<String> checkbox_merchandise) {
		this.checkbox_merchandise = checkbox_merchandise;
	}

	public String getMerStatus() {
		return merStatus;
	}

	public void setMerStatus(String merStatus) {
		this.merStatus = merStatus;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getKeywordsType() {
		return keywordsType;
	}

	public void setKeywordsType(String keywordsType) {
		this.keywordsType = keywordsType;
	}
	
	public Merchandise getMerchandise() {
		return merchandise;
	}

	public void setMerchandise(Merchandise merchandise) {
		this.merchandise = merchandise;
	}

	public List<DUnit> getdUnitsList() {
		return dUnitsList;
	}

	public void setdUnitsList(List<DUnit> dUnitsList) {
		this.dUnitsList = dUnitsList;
	}

	public List<RsVendor> getRsVendorsList() {
		return rsVendorsList;
	}

	public void setRsVendorsList(List<RsVendor> rsVendorsList) {
		this.rsVendorsList = rsVendorsList;
	}

	public ErpInventory getErpInventory() {
		return erpInventory;
	}

	public void setErpInventory(ErpInventory erpInventory) {
		this.erpInventory = erpInventory;
	}

	public ErpSalePrice getErpSalePrice() {
		return erpSalePrice;
	}

	public void setErpSalePrice(ErpSalePrice erpSalePrice) {
		this.erpSalePrice = erpSalePrice;
	}

	public List<ErpMerchandiseImage> getGoodsPicList() {
		return goodsPicList;
	}

	public void setGoodsPicList(List<ErpMerchandiseImage> goodsPicList) {
		this.goodsPicList = goodsPicList;
	}

	public ErpMerchandiseImage getErpMerchandiseImage() {
		return erpMerchandiseImage;
	}

	public void setErpMerchandiseImage(ErpMerchandiseImage erpMerchandiseImage) {
		this.erpMerchandiseImage = erpMerchandiseImage;
	}

	public ErpMerchandiseType getErpMerchandiseType() {
		return erpMerchandiseType;
	}

	public void setErpMerchandiseType(ErpMerchandiseType erpMerchandiseType) {
		this.erpMerchandiseType = erpMerchandiseType;
	}

	public String getDefImgPath() {
		return defImgPath;
	}

	public void setDefImgPath(String defImgPath) {
		this.defImgPath = defImgPath;
	}

	/**
	 * 查询商品列表
	 */
	@SuppressWarnings("unchecked")
	public String getMerchandList(){
		JSONObject jsonObject=new JSONObject();
		
		try{
			//检测是否拥有 权限
			/*boolean TYPE = this.coreAuthService.checkRigth(this.getSessionUser().getUserId(), "SHOP_GOODS_MANAGER_TYPE");
			boolean EDIT = this.coreAuthService.checkRigth(this.getSessionUser().getUserId(), "SHOP_GOODS_MANAGER_EDIT");
			boolean ADD = this.coreAuthService.checkRigth(this.getSessionUser().getUserId(), "SHOP_GOODS_MANAGER_ADD");*/
			boolean TYPE = true;
			boolean EDIT = true;
			boolean ADD =true;
			//是否出现按钮
			jsonObject.put("TYPE",TYPE) ;
			jsonObject.put("EDIT",EDIT) ;
			jsonObject.put("ADD",ADD) ;
			String shopId=request.getParameter("shopId");
			if(StringUtils.isBlank(shopId)){
				shopId=this.getSessionUser().getShopId();
			}
			String beginDate=request.getParameter("beginDate");
			String endDate=request.getParameter("endDate");
			String beginPrice=request.getParameter("beginPrice");
			String endPrice=request.getParameter("endPrice");
			String beginInventory=request.getParameter("beginInventory");
			String endInventory=request.getParameter("endInventory");
			String sysType=request.getParameter("sysType");
			String perType=request.getParameter("perType");
			String sort=request.getParameter("sort");
			String seoWeight=request.getParameter("seoWeight");
			String isWarmMer=request.getParameter("isWarmMer");
			resultList = this.merchandiseService.getQueryMerchandList(shopId,merStatus,keywordsType,keywords,page,beginDate,endDate,beginPrice,endPrice,beginInventory,endInventory,sysType,perType,seoWeight,sort,isWarmMer);
			
			JSONArray jsonArray=new JSONArray();
			
			HashMap<String, Object> hashMap;
			
			
			for(int i=0;i<resultList.size();i++){
				
				Merchandise merchandise=(Merchandise) resultList.get(i);
				
				hashMap=new HashMap<String, Object>();
				
				hashMap.put(Merchandise.COL_BARCODE, merchandise.getBarcode()==null?"":merchandise.getBarcode());
				
				hashMap.put(Merchandise.COL_MERCHANDISE_NAME, merchandise.getMerchandiseName()==null?"":merchandise.getMerchandiseName());
				
				hashMap.put("INVENTTORY", merchandise.getInventory()==null?"0":merchandise.getInventory());
				
				hashMap.put("COUNTSALES", merchandise.getCountSales()==null?"":merchandise.getCountSales());
				
				hashMap.put(Merchandise.COL_MKTPRICE, merchandise.getMktprice()==null?"":merchandise.getMktprice());
				
				hashMap.put("SALEPRICE", merchandise.getSalePrice()==null?"":merchandise.getSalePrice());
				
				hashMap.put(Merchandise.COL_MERCHANDISE_ID, merchandise.getMerchandiseId()==null?"":merchandise.getMerchandiseId());
				
				hashMap.put("merchandiseTypeName", (merchandise.getMerchandiseTypeName()==null?"":merchandise.getMerchandiseTypeName())+(merchandise.getMerchandiseTypeParentName()==null?"":"-"+merchandise.getMerchandiseTypeParentName()));
				
				hashMap.put("shopMerchandiseTypeName", (merchandise.getShopMerchandiseTypeName()==null?"":merchandise.getShopMerchandiseTypeName())+(merchandise.getShopMerchandiseTypeParentName()==null?"":"-"+merchandise.getShopMerchandiseTypeParentName()));
				
				hashMap.put("CLOSED",merchandise.getClosed());
				
				hashMap.put("RECOMMENDED", merchandise.getRecommended());
				
				hashMap.put("SEO_WEIGHT", merchandise.getSeoWeight());
				
				hashMap.put("UPDATE_TIME", merchandise.getUpdateTime()==null?"":DateToDString(merchandise.getUpdateTime()));
				
				
				jsonArray.add(hashMap);
				
			}
			
			jsonObject.put("merchandiseListData", jsonArray);
			
			jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
			
			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
			
			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
			
			jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
			
			
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		
		
		return this.writeJson(jsonObject);
		
	}
	
	
	
	
	/**
	 * 获取单位列表
	 * @return
	 */
	public String getDUnitsList(){
		

		JSONObject jsonObject=new JSONObject();

		JSONArray jsonArray=new JSONArray();
		
		String unitCode = request.getParameter("unitCode");
		String unitName = request.getParameter("unitName");
		
		try{
			//单位
			Map map = new HashMap();

			HashMap<String, Object> hashMap;
			
			
			formMap.put(DUnit.COL_UNIT_CODE, unitCode);
			formMap.put(DUnit.COL_UNIT_NAME, unitName);
			SqlUtis.prepareSql(formMap,
					SqlUtis.getSQL(formMap,"a."+DUnit.COL_UNIT_CODE,Types.VARCHAR,SqlUtis.LIKE),
					SqlUtis.getSQL(formMap,"a."+DUnit.COL_UNIT_NAME,Types.VARCHAR,SqlUtis.LIKE)
					);

			this.dUnitsList = this.dUnitService.list(formMap, page);
			for(int i=0;i<this.dUnitsList.size();i++){

				hashMap=new HashMap<String, Object>();

				hashMap.put("unitName", dUnitsList.get(i).getUnitName());

				hashMap.put("unitCode", dUnitsList.get(i).getUnitCode());

				jsonArray.add(hashMap);
			}

			jsonObject.put("dUnitsList", jsonArray);
			
			jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
			
			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
			
			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
			
			jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
			
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
		
		
	}
	
	
	/**
	 * 获取生产商列表
	 * @return
	 */
	public String getQueryRsVendorsList(){


		JSONObject jsonObject=new JSONObject();

		JSONArray jsonArray=new JSONArray();
		try{
			//生产商
			Map vendorMap = new HashMap();

			vendorMap.put(RsVendor.COL_TYPE, Constants.VENDOR_TYPE.GOODS_VENDOR);//商品生成厂家

			SqlUtis.prepareSql(vendorMap
					, SqlUtis.getSQL(vendorMap,"a."+RsVendor.COL_TYPE,Types.VARCHAR,SqlUtis.EQ)//商家ID
					);
			this.rsVendorsList = this.rsVendorService.list(vendorMap);

			HashMap<String, Object> hashMap;

			for(int i=0;i<this.rsVendorsList.size();i++){

				hashMap=new HashMap<String, Object>();

				hashMap.put("vendorId", rsVendorsList.get(i).getVendorId());

				hashMap.put("vendorName", rsVendorsList.get(i).getVendorName());

				jsonArray.add(hashMap);
			}

			jsonObject.put("rsVendorsList", jsonArray);

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);


	}

	
	/**
	 * 获取系统商品分类
	 * @return
	 */
	public String getErpMerchandiseTypesListSystem(){
		JSONObject jsonObject = new JSONObject();

		JSONArray jsonArray = new JSONArray();
		try{

			// 商品分类 公共类及商家自有分类
			Map typeMap = new HashMap();


				SqlUtis.prepareSql(typeMap, 
						" AND (a.SHOP_ID IS NULL) ORDER BY a.sort");

			this.erpMerchandiseTypesList = this.erpMerchandiseTypeService.list(typeMap);

			HashMap<String, Object> hashMap;

			for (int i = 0; i < this.erpMerchandiseTypesList.size(); i++) {

				hashMap = new HashMap<String, Object>();

				hashMap.put("merchandiseTypeId", erpMerchandiseTypesList.get(i)
						.getMerchandiseTypeId());

				hashMap.put("merchandiseTypeName", erpMerchandiseTypesList.get(i)
						.getMerchandiseTypeName());
				
				hashMap.put("shopId", erpMerchandiseTypesList.get(i).getShopId()==null?"":erpMerchandiseTypesList.get(i).getShopId());
				hashMap.put("parentId", erpMerchandiseTypesList.get(i).getParentId()==null?"":erpMerchandiseTypesList.get(i).getParentId());
				jsonArray.add(hashMap);
			}

			jsonObject.put("erpMerchandiseTypesListSystem", jsonArray);

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");

		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	
	
	
	/**
	 * 获取商品分类
	 * @return
	 */
	public String getErpMerchandiseTypesList() {

		JSONObject jsonObject = new JSONObject();

		JSONArray jsonArray = new JSONArray();
		try{
			String shopId = this.getSessionUser().getShopId();
			String type = request.getParameter("type");

			// 商品分类 公共类及商家自有分类
			Map typeMap = new HashMap();
			
			
			
			

				if(!StringUtils.isBlank(shopId)){
	
					if("noparent".equals(type)){
						SqlUtis.prepareSql(typeMap,  " AND (a.PARENT_ID IS NULL)",
								" AND ( a.SHOP_ID = '" + shopId + "') ORDER BY a.sort");
					}else{
						SqlUtis.prepareSql(typeMap, 
								" AND ( a.SHOP_ID = '" + shopId + "') ORDER BY a.sort");
						
					}
				}else{
	
					SqlUtis.prepareSql(typeMap, " AND a.PARENT_ID is NULL AND a.SHOP_ID IS NULL ORDER BY a.sort");
	
	
				}
				
			this.erpMerchandiseTypesList = this.erpMerchandiseTypeService.list(typeMap);

			HashMap<String, Object> hashMap;

			for (int i = 0; i < this.erpMerchandiseTypesList.size(); i++) {

				hashMap = new HashMap<String, Object>();

				hashMap.put("merchandiseTypeId", erpMerchandiseTypesList.get(i)
						.getMerchandiseTypeId());

				hashMap.put("merchandiseTypeName", erpMerchandiseTypesList.get(i)
						.getMerchandiseTypeName());
				hashMap.put("shopId", erpMerchandiseTypesList.get(i).getShopId()==null?"":erpMerchandiseTypesList.get(i).getShopId());
				hashMap.put("parentId", erpMerchandiseTypesList.get(i).getParentId()==null?"":erpMerchandiseTypesList.get(i).getParentId());
				
				jsonArray.add(hashMap);
			}

			jsonObject.put("erpMerchandiseTypesList", jsonArray);

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");

		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	
	
	
	
	
	/**
	 * 通过商品id ，查询商品详情	
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String getMerchandDetail(){
//		String merchandiseId = request.getParameter("merchandiseId");
//		String shopId = this.getSessionUser().getShopId();
		/*request.setAttribute("resultInfo",request.getParameter("resultInfo"));
		this.checkRight(AUTH_SHOP_INVENTORY_MANAGER);//检查权限
		if(StringUtils.isBlank(this.getSessionUser().getShopId())){
			this.saveActionError("商户编码不能为空");
			return "/shop/goodsDetail.jsp";
		}*/
//		if(merchandiseId!=null&&merchandiseId!=""){
//			formMap.put(Merchandise.COL_MERCHANDISE_ID, merchandiseId);
//		}
//		formMap.put(Merchandise.COL_SHOP_ID, shopId);
//		SqlUtis.prepareSql(formMap
//				, SqlUtis.getSQL(formMap,"a."+Merchandise.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),//商家ID
//				SqlUtis.getSQL(formMap,"a."+Merchandise.COL_MERCHANDISE_ID,Types.VARCHAR,SqlUtis.EQ)//商品id
//				);
//		Map map = new HashMap();
		//单位
//		this.dUnitsList = this.dUnitService.list(map);
//		request.setAttribute("dUnitsList", dUnitsList);
		//生产商
//		Map vendorMap = new HashMap();
//		vendorMap.put(RsVendor.COL_TYPE, Constants.VENDOR_TYPE.GOODS_VENDOR);//商品生成厂家
//		SqlUtis.prepareSql(vendorMap
//				, SqlUtis.getSQL(vendorMap,"a."+RsVendor.COL_TYPE,Types.VARCHAR,SqlUtis.EQ)//商家ID
//				);
//		this.rsVendorsList = this.rsVendorService.list(vendorMap);
//		request.setAttribute("rsVendorsList", rsVendorsList);
//		Map typeMap=new HashMap();
		//商品分类
//		SqlUtis.prepareSql(typeMap,
//				" AND (a.PARENT_ID IS NULL)",
//				" AND (a.SHOP_ID IS NULL",
//				" OR a.SHOP_ID = '"+shopId+"')"
//				);
//		this.erpMerchandiseTypesList = this.erpMerchandiseTypeService.list(typeMap);
//		request.setAttribute("erpMerchandiseTypesList", erpMerchandiseTypesList);
//		Map goodPicMap = new HashMap();
//		//商品图片
//		goodPicMap.put(ErpMerchandiseImage.COL_GOODS_ID, merchandiseId);
//		SqlUtis.prepareSql(goodPicMap
//				, SqlUtis.getSQL(goodPicMap,"a."+ErpMerchandiseImage.COL_GOODS_ID,Types.VARCHAR,SqlUtis.EQ)//商家ID
//				);
//		this.goodsPicList = this.erpMerchandiseImageService.list(goodPicMap);
//		request.setAttribute("goodsPicList", goodsPicList);
//		//默认图片
//		goodPicMap = new HashMap();
//		goodPicMap.put(ErpMerchandiseImage.COL_GOODS_ID, merchandiseId);
//		goodPicMap.put(ErpMerchandiseImage.COL_DEF, "1");
//		SqlUtis.prepareSql(goodPicMap
//				, SqlUtis.getSQL(goodPicMap,"a."+ErpMerchandiseImage.COL_GOODS_ID,Types.VARCHAR,SqlUtis.EQ),
//				SqlUtis.getSQL(goodPicMap,"a."+ErpMerchandiseImage.COL_DEF,Types.VARCHAR,SqlUtis.EQ)//商家ID
//				);
//		this.erpMerchandiseImage=this.erpMerchandiseImageService.one(goodPicMap);
//		request.setAttribute("erpMerchandiseImage",erpMerchandiseImage);
//		this.merchandise = this.merchandiseService.one(formMap);
//		//查询父级分类
//		if(merchandise.getMerchandiseTypeId()!=null){
//			typeMap.put(ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID, merchandise.getMerchandiseTypeId());
//			SqlUtis.prepareSql(typeMap,
//					SqlUtis.getSQL(typeMap, "a."+ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID,Types.VARCHAR,SqlUtis.EQ)
//					);
//			this.erpMerchandiseType = this.erpMerchandiseTypeService.one(typeMap);
//		}
//		request.setAttribute("shopId", this.getSessionUser().getShopId());
//		return "/shop/goodsDetail.jsp";
//		
		
		JSONObject jsonObject=new JSONObject();
		
		try{
			
		String merchandiseId = request.getParameter("merchandiseId");
		String shopId = this.getSessionUser().getShopId();
		/*request.setAttribute("resultInfo",request.getParameter("resultInfo"));
		this.checkRight(AUTH_ADMIN_INVENTORY_MANAGER);//检查权限
		if(StringUtils.isBlank(this.getSessionUser().getUserId())){
			this.saveActionError("管理员编码不能为空");
			return "/admin/goodsManage/goodsDetail.jsp";
		}*/
		
		
		JSONArray jsonArray=new JSONArray();
		

		jsonArray=new JSONArray();
		//request.setAttribute("goodsPicList", goodsPicList);
		//默认图片
		this.erpMerchandiseImage=this.merchandiseService.getMerchandiseImage(merchandiseId);
		
		if(erpMerchandiseImage!=null){
			
			jsonObject.put("imageId", erpMerchandiseImage.getImageId());
			
			jsonObject.put("imageUrl", erpMerchandiseImage.getImageUrl());
		}

		
		
		this.merchandise = this.merchandiseService.erpMerchandiseDetail(shopId,merchandiseId);
		
		jsonObject.put("seoWeight", merchandise.getSeoWeight());
		
		jsonObject.put("barcode", merchandise.getBarcode());

		jsonObject.put("merchandiseName", merchandise.getMerchandiseName());
		
		jsonObject.put("brief", merchandise.getBrief());
		
		jsonObject.put("merchandiseTypeId", merchandise.getMerchandiseTypeId());
		
		jsonObject.put("shopMerchandiseTypeId", merchandise.getShopMerchandiseTypeId());
		
		jsonObject.put("unitName", merchandise.getUnitName());

		jsonObject.put("inventory", merchandise.getInventory());
		
		jsonObject.put("vendorName", merchandise.getVendorName());
		
		jsonObject.put("costPrice", merchandise.getCostPrice());
		
		jsonObject.put("mktprice", merchandise.getMktprice());
		
		jsonObject.put("salePrice", merchandise.getSalePrice());
		
		jsonObject.put("detail", merchandise.getDetail());
		
		jsonObject.put("closed", merchandise.getClosed());
		
		jsonObject.put("recommended", merchandise.getRecommended());

		jsonObject.put("inventoryWarn", merchandise.getInventoryWarn());
		
		jsonObject.put("memo", merchandise.getMemo());
		
		jsonObject.put("detail", merchandise.getDetail());
		
		jsonObject.put("baseShopId", this.getSessionUser().getShopId()==null?"":EncryptUtil.encodeBase64(this.getSessionUser().getShopId().trim()));//加密shopId,用于生成二维码
		jsonObject.put("shopName", this.getSessionUser().getShopName()==null?"":this.getSessionUser().getShopName());
		jsonObject.put("baseMerchandiseId", merchandise.getMerchandiseId()==null?"":EncryptUtil.encodeBase64(merchandise.getMerchandiseId().trim()));//加密merchandiseId,用于生成二维码
		
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
		
		
	}
	
	/**
	 * 修改商品信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String saveGoodsInfoById(){
//		Map mapInfoMap = new HashMap();
//		mapInfoMap.put("goodsPicList", goodsPicList);
//		mapInfoMap.put("erpMerchandiseImageService", erpMerchandiseImageService);
//		mapInfoMap.put("merchandise", merchandise);
//		mapInfoMap.put("shopId", this.getSessionUser().getShopId());
//		mapInfoMap.put("erpInventory", erpInventory);
//		mapInfoMap.put("erpInventoryService", erpInventoryService);
//		mapInfoMap.put("erpSalePrice", erpSalePrice);
//		mapInfoMap.put("erpSalePriceService", erpSalePriceService);
//		mapInfoMap.put("defImgPath",defImgPath);
//		Integer resultInfo = this.merchandiseService.modifyGoodsInfo(mapInfoMap);
//		this.logService.addShopAuditLog(AUTH_SHOP_INVENTORY_MANAGER, "修改商品信息");
//		return "!/shop/Merchandise/getMerchandDetail.do?merchandiseId="+merchandise.getMerchandiseId()+"&"
//				+ "resultInfo="+resultInfo;
		
		JSONObject jsonObject=new JSONObject();
		try{
			
			String merchandiseId=request.getParameter("merchandiseId");
			String barcode=request.getParameter("barcode");
			String shopId=this.getSessionUser().getShopId();
			String merchandiseName=request.getParameter("merchandiseName");
			String brief=request.getParameter("brief");
			String merchandiseTypeId=request.getParameter("merchandiseTypeId");
			String	shopMerchandiseTypeId=request.getParameter("perType");
			
			String unitName=request.getParameter("unitName");
			String inventory=request.getParameter("inventory");
			String costPrice=request.getParameter("costPrice");
			String mktprice=request.getParameter("mktprice");
			String salePrice=request.getParameter("salePrice");
			
			String vendorName=request.getParameter("vendorName");
			String detail=request.getParameter("detail");
			String closed=request.getParameter("closed");
			String recommended = request.getParameter("recommended");
			String inventoryWarn=request.getParameter("inventoryWarn");
			String seoWeight = request.getParameter("seoWeight");
			
			
			Map mapInfoMap = new HashMap();
			mapInfoMap.put("goodsPicList", goodsPicList);
			mapInfoMap.put("erpMerchandiseImageService", erpMerchandiseImageService);
			mapInfoMap.put("erpInventory", erpInventory);
			mapInfoMap.put("erpInventoryService", erpInventoryService);
			mapInfoMap.put("erpSalePrice", erpSalePrice);
			mapInfoMap.put("erpSalePriceService", erpSalePriceService);
			mapInfoMap.put("defImgPath",defImgPath);
			
			
			this.merchandiseService.modifyGoodsInfo(merchandiseId,barcode,shopId,merchandiseName,brief,merchandiseTypeId,shopMerchandiseTypeId,unitName,
					inventory,costPrice,mktprice,salePrice,vendorName,detail,closed,recommended,inventoryWarn,mapInfoMap,seoWeight);
			
//			this.merchandiseService.modifyGoodsInfo(mapInfoMap);
			this.logService.addShopAuditLog(AUTH_SHOP_INVENTORY_MANAGER, "修改商品信息");
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
	
		return this.writeJson(jsonObject);
		
		
		
		
		
		
		
		
		
	}
	/**
	 * 新增商品信息 -页面
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String addGoodsInfoPage(){
		String shopId = this.getSessionUser().getShopId();
		Map map = new HashMap();
		this.dUnitsList = this.dUnitService.list(map);
		request.setAttribute("dUnitsList", dUnitsList);
		Map vendorMap = new HashMap();
		vendorMap.put(RsVendor.COL_TYPE, Constants.VENDOR_TYPE.GOODS_VENDOR);//商品生成厂家
		SqlUtis.prepareSql(vendorMap
				, SqlUtis.getSQL(vendorMap,"a."+RsVendor.COL_TYPE,Types.VARCHAR,SqlUtis.EQ)//商家ID
				);
		this.rsVendorsList = this.rsVendorService.list(vendorMap);
		request.setAttribute("rsVendorsList", rsVendorsList);
		Map typeMap=new HashMap();
		SqlUtis.prepareSql(typeMap,
				" AND (a.PARENT_ID IS NULL)",
				" AND (a.SHOP_ID IS NULL",
				" OR a.SHOP_ID = '"+shopId+"')"
				);
		this.erpMerchandiseTypesList = this.erpMerchandiseTypeService.list(typeMap);
		request.setAttribute("erpMerchandiseTypesList", erpMerchandiseTypesList);
		request.setAttribute("rsVendorsList", rsVendorsList);
		request.setAttribute("shopId", this.getSessionUser().getShopId());
		request.setAttribute("resultInfo",request.getParameter("resultInfo"));
		return "/shop/goodsAdd.jsp";
	}
	/**
	 * 保存 新增商品信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String addGoodsInfo(){
//		Map mapInfoMap = new HashMap();
//		merchandise.setShopId(this.getSessionUser().getShopId());
//		mapInfoMap.put("goodsPicList", goodsPicList);
//		mapInfoMap.put("erpMerchandiseImageService", erpMerchandiseImageService);
//		mapInfoMap.put("merchandise", merchandise);
//		mapInfoMap.put("shopId", this.getSessionUser().getShopId());
//		mapInfoMap.put("erpInventory", erpInventory);
//		mapInfoMap.put("erpInventoryService", erpInventoryService);
//		mapInfoMap.put("erpSalePrice", erpSalePrice);
//		mapInfoMap.put("erpSalePriceService", erpSalePriceService);
//		mapInfoMap.put("defImgPath", defImgPath);
//		mapInfoMap.put("userId", this.getSessionUser().getUserId());
//		Integer resultInfo = this.merchandiseService.addGoodsInfo(mapInfoMap);
//		this.logService.addShopAuditLog(AUTH_SHOP_INVENTORY_MANAGER, "新增商品信息");
//		return "!/shop/Merchandise/addGoodsInfoPage.do?resultInfo="+resultInfo;
		
		
		
		
		String barcode=request.getParameter("barcode");
		String shopId=this.getSessionUser().getShopId();
		String merchandiseName=request.getParameter("merchandiseName");
		String brief=request.getParameter("brief");
		String merchandiseTypeId=request.getParameter("merchandiseTypeId");
		String shopMerchandiseTypeId=request.getParameter("perType");
		String unitName=request.getParameter("unitName");
		String inventory=request.getParameter("inventory");
		String costPrice=request.getParameter("costPrice");
		String mktprice=request.getParameter("mktprice");
		String salePrice=request.getParameter("salePrice");
		String closed=request.getParameter("closed");
		String recommended=request.getParameter("recommended");
		String virtual="0";//默认普通商品
		String inventoryWarn=request.getParameter("inventoryWarn");
		String vendorName=request.getParameter("vendorName");
		String detail=request.getParameter("detail");
		String seoWeight = request.getParameter("seoWeight");
		String defImgPath = request.getParameter("defImgPath");
		JSONObject jsonObject=new JSONObject();
		try{
			

			formMap.put("shop_Id", shopId);
			
			formMap.put("barcode", barcode);
			
			if(!(barcode==null || barcode.equals(""))){
				
				SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a.shop_Id", Types.VARCHAR, SqlUtis.EQ), SqlUtis.getSQL(formMap, "a.barcode", Types.VARCHAR, SqlUtis.EQ));
				
				boolean exists=this.merchandiseService.exists(formMap);
				
				if(exists){
				
					jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
					
					jsonObject.put(ErrorMsg.KEY_MESSAGE,"商家的商品条形码重复,请重新输入!");
					
					return this.writeJson(jsonObject);
	
				}
			}
			
			Map mapInfoMap = new HashMap();
			mapInfoMap.put("goodsPicList", goodsPicList);
			mapInfoMap.put("erpMerchandiseImageService", erpMerchandiseImageService);
			mapInfoMap.put("merchandise", merchandise);
			mapInfoMap.put("defImgPath", defImgPath);
			
			mapInfoMap.put("shopId", this.getSessionUser().getShopId());
			mapInfoMap.put("erpInventory", erpInventory);
			mapInfoMap.put("erpInventoryService", erpInventoryService);
			mapInfoMap.put("erpSalePrice", erpSalePrice);
			mapInfoMap.put("erpSalePriceService", erpSalePriceService);
			mapInfoMap.put("userId", this.getSessionUser().getUserId());
			
			
			this.merchandiseService.addGoodsInfo(barcode,shopId,merchandiseName,brief,merchandiseTypeId,shopMerchandiseTypeId,unitName,
					inventory,costPrice,mktprice,salePrice,vendorName,detail,closed,recommended,virtual,inventoryWarn,mapInfoMap,seoWeight);
			
			
//			String merchandiseId=this.merchandiseService.insertGoodsInfoStepOne(barcode,shopId,merchandiseName,brief,merchandiseTypeId,shopMerchandiseTypeId,unitName,
//					inventory,costPrice,mktprice,salePrice,closed,recommended,inventoryWarn,vendorName);
			
			
//			jsonObject.put("merchandiseId", merchandiseId);
			
			this.logService.addShopAuditLog(AUTH_SHOP_INVENTORY_MANAGER, "新增商品信息成功");
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
	
		return this.writeJson(jsonObject);
	}
	/**
	 * 通过id,查询二级分类
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String findTypeForId(){
//		Map mapInfo = new HashMap();
//		mapInfo.put(ErpMerchandiseType.COL_PARENT_ID, request.getParameter("parentId"));
////		mapInfo.put(ErpMerchandiseType.COL_SHOP_ID, request.getParameter("shopId"));
//		SqlUtis.prepareSql(mapInfo
//				, SqlUtis.getSQL(mapInfo,"a."+ErpMerchandiseType.COL_PARENT_ID,Types.VARCHAR,SqlUtis.EQ)
//				/* ,SqlUtis.getSQL(mapInfo,"a."+ErpMerchandiseType.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ)*/
//				);
//		List<ErpMerchandiseType> erpMerchandiseTypeList = this.erpMerchandiseTypeService.list(mapInfo);
//		JSONArray jsonOb= JSONArray.fromObject(erpMerchandiseTypeList);
//		return this.writeJson(jsonOb);
		
		Map mapInfo = new HashMap();
		mapInfo.put(ErpMerchandiseType.COL_PARENT_ID, request.getParameter("parentId"));
	//	mapInfo.put(ErpMerchandiseType.COL_SHOP_ID, request.getParameter("shopId"));
		SqlUtis.prepareSql(mapInfo
				, SqlUtis.getSQL(mapInfo,"a."+ErpMerchandiseType.COL_PARENT_ID,Types.VARCHAR,SqlUtis.EQ)
				 ,SqlUtis.getSQL(mapInfo,"a."+ErpMerchandiseType.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),
				 "order by MERCHANDISE_TYPE_ID "
				);
		List<ErpMerchandiseType> erpMerchandiseTypeList = this.erpMerchandiseTypeService.list(mapInfo);
		
		HashMap<String, Object> hashMap;
		
		JSONArray jsonArray=new JSONArray();

		JSONObject jsonObject=new JSONObject();
		
		for(int i=0;i<erpMerchandiseTypeList.size();i++){
			
			hashMap=new HashMap<String, Object>();
			
			hashMap.put("merchandiseTypeId", erpMerchandiseTypeList.get(i).getMerchandiseTypeId());
			
			hashMap.put("merchandiseTypeName", erpMerchandiseTypeList.get(i).getMerchandiseTypeName());
			
			jsonArray.add(hashMap);
		}
	
		jsonObject.put("erpMerchandiseTypesList", jsonArray);
		
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		
		return this.writeJson(jsonObject);
		
		
		
		
		
		
		
		
	}
	/**
	 * 查询商品条码是否存在
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String existsBarCode(){
		Map mapInfo = new HashMap();
		String resultExists="false";
		mapInfo.put(Merchandise.COL_BARCODE, request.getParameter("barCode"));
		mapInfo.put(Merchandise.COL_SHOP_ID,  this.getSessionUser().getShopId());
		SqlUtis.prepareSql(mapInfo
				, SqlUtis.getSQL(mapInfo,"a."+Merchandise.COL_BARCODE,Types.VARCHAR,SqlUtis.EQ),//条形码
				SqlUtis.getSQL(mapInfo,"a."+Merchandise.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ)
				);
		boolean barCodeExists = this.merchandiseService.exists(mapInfo);
		if(barCodeExists){
			resultExists="true";
		}
		JSONObject jsonOb= new JSONObject();
		jsonOb.put("resultExists", resultExists);
		return this.writeJson(jsonOb);
		
	}
	
	
	
	
	/**
	 * 获取商品图片
	 * @return
	 */
	public String getGoodsPhotoList() {
		

		String merchandiseId = request.getParameter("merchandiseId");
		
		JSONObject jsonObject = new JSONObject();

		JSONArray jsonArray = new JSONArray();

		try{
		//商品图片
		Map goodPicMap = new HashMap();
		goodPicMap.put(ErpMerchandiseImage.COL_GOODS_ID, merchandiseId);
		SqlUtis.prepareSql(goodPicMap
				, SqlUtis.getSQL(goodPicMap,"a."+ErpMerchandiseImage.COL_GOODS_ID,Types.VARCHAR,SqlUtis.EQ)//商品代码
				);
		this.goodsPicList = this.erpMerchandiseImageService.list(goodPicMap);
		HashMap<String, Object> hashMap;
		for(int i=0;i<this.goodsPicList.size();i++){
			
			hashMap=new HashMap<String, Object>();
			
			hashMap.put("imageUrl", goodsPicList.get(i).getImageUrl());
			
			hashMap.put("imageId", goodsPicList.get(i).getImageId());
			
			hashMap.put("def", goodsPicList.get(i).getDef());
			
			jsonArray.add(hashMap);
		}
		
		
		jsonObject.put("goodsPicList", jsonArray);

		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

		jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");

	}catch(Exception e){
		
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用失败");
		e.printStackTrace();
	}
		return this.writeJson(jsonObject);
	}
	
	
	
	
	
	
	
	
	/**
	 * 删除商品图片
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String delPic(){
//		Map mapInfo = new HashMap();
//		mapInfo.put(ErpMerchandiseImage.COL_IMAGE_ID, request.getParameter("picId"));
//		mapInfo.put(ErpMerchandiseImage.COL_GOODS_ID, request.getParameter("goodsId"));
//		mapInfo.put("erpMerchandiseImageService", this.erpMerchandiseImageService);
//		Integer resultDel = this.merchandiseService.delPic(mapInfo);
//		JSONObject jsonOb= new JSONObject();
//		jsonOb.put("resultDel", resultDel);
//		return this.writeJson(jsonOb);
		
		JSONObject jsonObject=new JSONObject();
		try{
			
			String imageId=request.getParameter("image_id");
			
		this.merchandiseService.delPic(imageId,this.erpMerchandiseImageService);		
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
		jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
		
		
	}
	
	
	/**
	 * 查询商品分类列表
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getMerchandiseTypeList() {

		JSONObject jsonObject = new JSONObject();
		try {
			
			String shopId=this.getSessionUser().getShopId();
			
			String merchandiseTypeId=this.request.getParameter("merchandiseTypeId");
			
			String merchandiseTypeName=this.request.getParameter("merchandiseTypeName");
			
			List<MerchandiseType> list=this.merchandiseService.getMerchandiseTypeList(shopId,merchandiseTypeId,merchandiseTypeName);
			
			HashMap<String, Object> hashMap;
			
			JSONArray jsonArray=new JSONArray();
			
			for(int i=0;i<list.size();i++){
				
				hashMap=new HashMap<String, Object>();
				
				hashMap.put(MerchandiseType.COL_MERCHANDISE_TYPE_ID, list.get(i).getMerchandiseTypeId());
				
				hashMap.put(MerchandiseType.COL_MERCHANDISE_TYPE_NAME, list.get(i).getMerchandiseTypeName());
				
				hashMap.put(MerchandiseType.COL_PARENT_ID, list.get(i).getParentId());
				
				hashMap.put("PARENT_NAME", list.get(i).getParentName());
				
				hashMap.put(MerchandiseType.COL_SHOP_ID, list.get(i).getShopId());

				hashMap.put("SHOP_NAME", list.get(i).getShopName());

				if(list.get(i).getParentId()==null){
					hashMap.put("merNum", list.get(i).getSumMerNum()==null?"0":list.get(i).getSumMerNum());
				}else{
					hashMap.put("merNum", list.get(i).getMerNum()==null?"0":list.get(i).getMerNum());
				}
				
				
				jsonArray.add(hashMap);
			}
			
			jsonObject.put("merchandiseTypeList", jsonArray);
			
			jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
			
			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
			
			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
			
			jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
			
			
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");
		} catch (Exception e) {

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用失败");
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 添加商品分类
	 * @return
	 */
	public String addMerchandiseType() {

		JSONObject jsonObject = new JSONObject();
		try {
			String parentId=this.request.getParameter("parentId");
			String merchandiseTypeName=this.request.getParameter("merchandiseTypeName");
			String shopId=this.getSessionUser().getShopId();
			String merchandiseTypeId=this.merchandiseService.insertMerchandiseType(parentId, merchandiseTypeName, shopId).toString();
			jsonObject.put("merchandiseTypeId", merchandiseTypeId);
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");
		} catch (Exception e) {

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用失败");
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 修改商品分类
	 * @return
	 */
	public String updateMerchandiseType() {

		JSONObject jsonObject = new JSONObject();
		try {
			String merchandiseTypeId=this.request.getParameter("merchandiseTypeId");
			String parentId=this.request.getParameter("parentId");
			String merchandiseTypeName=this.request.getParameter("merchandiseTypeName");
			String shopId=this.request.getParameter("shopId");
			this.merchandiseService.updateMerchandiseType(merchandiseTypeId, parentId, merchandiseTypeName, shopId);
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");
		} catch (Exception e) {

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用失败");
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 删除没有被关联的商品分类
	 * @return
	 */
	public String deleteMerchandiseType(){
		JSONObject jsonObject = new JSONObject();
		try {
			String merchandiseTypeId=this.request.getParameter("merchandiseTypeId");
			String operType = this.request.getParameter("operType");
			
			//在分类树中检测分类是否管理商品
			if("checkMerNum".equals(operType)){
				
				boolean exist=this.merchandiseService.checkMerchandiseType(merchandiseTypeId);
				jsonObject.put("merNum", !exist);
				return this.writeJson(jsonObject);
			}
			
			boolean exist=this.merchandiseService.checkMerchandiseType(merchandiseTypeId);
			if(!exist){
				
				boolean existParent=this.merchandiseService.checkMerchandiseTypeParent(merchandiseTypeId);
				if(!existParent){
					boolean falg=this.merchandiseService.deleteMerchandiseType(merchandiseTypeId);
					if(falg){
						jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
						jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");
					}else{
						jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
						jsonObject.put(ErrorMsg.KEY_MESSAGE, "删除失败");
					}
				}else{
					jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
					jsonObject.put(ErrorMsg.KEY_MESSAGE, "该商品分类已被其他分类关联，不允许删除");
				}
				
			}else{
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE, "该商品分类已被商品关联，不允许删除");
			}
			
			
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 检查条形码是否存在
	 * @return
	 */
	public String checkBarcode(){
		JSONObject jsonObject = new JSONObject();
		HashMap<String, Object> hashMap;
		JSONArray jsonArray=new JSONArray();
		try {
			String barcode=request.getParameter("barcode");
			String shopId=this.getSessionUser().getShopId();
			
			boolean exits=this.merchandiseService.checkBarcode(barcode, shopId);
			if(!exits){
				
				ErpBarcode erpBarcode = this.erpBarcodeService.one(barcode);
				
				if(erpBarcode!=null){
					jsonObject.put(ErpBarcode.COL_NAME, erpBarcode.getName()==null?"":erpBarcode.getName());
					jsonObject.put(ErpBarcode.COL_MKTPRICE, erpBarcode.getMktprice()==null?"":erpBarcode.getMktprice());
					jsonObject.put(ErpBarcode.COL_BRIEF, erpBarcode.getBrief()==null?"":erpBarcode.getBrief());
					jsonObject.put(ErpBarcode.COL_MERCHANDISE_TYPE_ID, erpBarcode.getMerchandiseTypeId()==null?"":erpBarcode.getMerchandiseTypeId());
					jsonObject.put(ErpBarcode.COL_UNIT_NAME, erpBarcode.getUnitName()==null?"":erpBarcode.getUnitName());
					jsonObject.put(ErpBarcode.COL_VENDOR_NAME, erpBarcode.getVendorName()==null?"":erpBarcode.getVendorName());
				}else{
					jsonObject.put("nullErpBarcode", "nullErpBarcode");
				}
				
				Map map = new HashMap();
				map.put(ErpBarcodeImage.COL_BARCODE, barcode);
				SqlUtis.prepareSql(map, SqlUtis.getSQL(map, ErpBarcodeImage.COL_BARCODE,Types.VARCHAR,SqlUtis.EQ));
				List<ErpBarcodeImage> erpBarcodeImageList = this.erpBarcodeImageService.list(map);
				
				for (int i = 0; i < erpBarcodeImageList.size(); i++) {
					hashMap=new HashMap<String, Object>();
					hashMap.put("imageUrl", erpBarcodeImageList.get(i).getImageUrl());
					hashMap.put("imageId", erpBarcodeImageList.get(i).getImageId());
					hashMap.put("def", erpBarcodeImageList.get(i).getDef());
					jsonArray.add(hashMap);
				}
				jsonObject.put("barcodePicList", jsonArray);
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
				jsonObject.put(ErrorMsg.KEY_MESSAGE, "调用成功");
			}else{
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE, "条形码已存在，请重新输入！");
			}
			
			
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		
	
		return this.writeJson(jsonObject);
		
	}
	
	/**
	 * 批量上下架商品
	 * @return
	 */
	public String batchUpdateClose(){
		JSONObject jsonObject=new JSONObject();
		

		try{
		
		String closed=request.getParameter("closed");
			
	    this.merchandiseService.batchUpdateCloseService(checkbox_merchandise, closed);
			
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

		jsonObject.put(ErrorMsg.KEY_MESSAGE,"批量更新成功");
		
		}catch(Exception e){
			
			e.printStackTrace();
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"批量更新失败");
				
			}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 批量上下架商品
	 * @return
	 */
	public String batchUpdateRecommend(){
		JSONObject jsonObject=new JSONObject();
		
		try{
		
		String recommend=request.getParameter("recommend");
			
	    this.merchandiseService.batchUpdateRecommendService(checkbox_merchandise, recommend);
			
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

		jsonObject.put(ErrorMsg.KEY_MESSAGE,"批量更新成功");
		
		}catch(Exception e){
			
			e.printStackTrace();
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"批量更新失败");
				
			}
		return this.writeJson(jsonObject);
	}
	
	public String updateMerchandiseField(){
		
		JSONObject jsonObject=new JSONObject();
		
		try{
		
		String merchandiseId=request.getParameter("merchandiseId");
		String merchandiseName=request.getParameter("merchandiseName");
		String mktprice=request.getParameter("mktprice");
		String inventory=request.getParameter("inventory");
		String salePrice=request.getParameter("salePrice");
			
	    this.merchandiseService.updateMerchandiseFieldService(merchandiseId, merchandiseName, mktprice, inventory, salePrice);
			
		jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

		jsonObject.put(ErrorMsg.KEY_MESSAGE,"更新成功");
		
		}catch(Exception e){
			
			e.printStackTrace();
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"更新失败");
				
			}
		return this.writeJson(jsonObject);
		
	}
	
	/*
	 * 保存分类排序
	 */
	public String updateMerchandiseTypeSort(){
		JSONObject jsonObject=new JSONObject();
		
		//检测权限
		if(!this.coreAuthService.checkRigth(this.getSessionUser().getUserId(), "SHOP_GOODS_MANAGER_TYPETREE")){
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"您没有操作此功能的权限！");
			return this.writeJson(jsonObject);
		}
		
		
		
		try{

			for (int i = 0; i < merchandiseTypeIdArr.size(); i++) {
				
				ErpMerchandiseType tempMerType = new ErpMerchandiseType();
				tempMerType.setMerchandiseTypeId(merchandiseTypeIdArr.get(i));
				if(merchandiseTypeParentIdArr.get(i).equals("null")){
					tempMerType.setParentId("");
				}else{
					tempMerType.setParentId(merchandiseTypeParentIdArr.get(i));
				}
				tempMerType.setSort(Integer.parseInt(merchandiseTypeSortArr.get(i)));
				this.erpMerchandiseTypeService.update(tempMerType);
			}

			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"更新成功");
			
			}catch(Exception e){
				
				e.printStackTrace();
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);

				jsonObject.put(ErrorMsg.KEY_MESSAGE,"更新失败");
					
				}
			return this.writeJson(jsonObject);
	}
	
}
